package org.tigris.atlas.xml.schema.mda.element;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.tigris.atlas.mda.PackageManager;
import org.tigris.atlas.mda.metadata.element.Composite;
import org.tigris.atlas.mda.metadata.element.Field;

public class SchemaComposite implements Composite, NamespaceAware {
	
	private Composite composite;
	private List decoratedFieldList;
	
	/**
	 * Create a new instance of <tt>Composite</tt> with the correct functionality set 
	 * to generate Schema code
	 * @param compositeToDecorate The <tt>Composite</tt> to decorate
	 */
	public SchemaComposite(Composite compositeToDecorate) {
		if (compositeToDecorate == null) {
			throw new IllegalArgumentException("SchemaComposites must be instatiated with a non-null composite!");
		}
		composite = compositeToDecorate;
	}	

	public String getPrefix() {
		return composite.getPrefix();
	}

	public String getType() {
		return composite.getType();
	}

	public String getProject() {
		return composite.getProject();
	}

	public String getCapitalizedName() {
		return StringUtils.capitalize(composite.getName());
	}

	public Collection getFields() {
		if (decoratedFieldList == null) {
			Collection compositeFieldList = composite.getFields();
			if ((compositeFieldList == null) || (compositeFieldList.size() == 0)) {
				decoratedFieldList = Collections.EMPTY_LIST;
				
			} else {
				Field f;
				decoratedFieldList = new ArrayList(compositeFieldList.size());
				Iterator i = compositeFieldList.iterator();
				while (i.hasNext()) {
					f = (Field)i.next();
					decoratedFieldList.add(new SchemaField(f));
					
				}
				
			}
		}
		
		return decoratedFieldList;
	}

	public String getName() {
		return composite.getName();
	}
	
	/**
	 * @see org.tigris.atlas.mda.metadata.element.Composite#getApplicationName()
	 */
	public String getApplicationName() {
		return composite.getApplicationName();
	}
	
	//////////////////////////////////////////////////////////////////////
	//schema-specific methods:
	//////////////////////////////////////////////////////////////////////

	/**
	 * Returns the schema type for this entity
	 */
	public String getSchemaType() {		
		return SchemaElementUtils.getComplexTypeSchemaType(getType());
	}
	
	/**
	 * Return namespace name of this instance (ie, the 'xs' part of 'xmlns:xs')
	 */
	public String getNamespaceName() {
		String name = SchemaElementUtils.getNamespaceName(null, getType());
		return name;
	}
	
	/**
	 * Returns relative location of the namespace
	 */
	public String getNamespaceLocation() {
		String location = SchemaElementUtils.getNamespaceLocation(null, getType());
		return location;
	}

	public String getLabel() {
		return composite.getLabel();
	}		
	
	/**
	 * Returns relative location of the namespace
	 */
	public String getBase() {
		String basePackage = PackageManager.getBasePackage( getProject() );
		String baseNamespace = SchemaElementUtils.translateBasePackageToBaseSchemaLocation(basePackage); 
		return baseNamespace;
	}	

}
